package org.dromara.business.service.impl;

import org.dromara.business.domain.LzshBusinessInfo;
import org.dromara.business.domain.LzshBusinessInfoDataTotal;
import org.dromara.business.domain.LzshBusinessMenuNum;
import org.dromara.business.mapper.LzshBusinessInfoMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.LzshBusinessMenuBo;
import org.dromara.business.domain.vo.LzshBusinessMenuVo;
import org.dromara.business.domain.LzshBusinessMenu;
import org.dromara.business.mapper.LzshBusinessMenuMapper;
import org.dromara.business.service.ILzshBusinessMenuService;

import java.util.*;
import java.util.List;

/**
 * 商机填报系统菜单Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-22
 */
@RequiredArgsConstructor
@Service
public class LzshBusinessMenuServiceImpl implements ILzshBusinessMenuService {

    private final LzshBusinessMenuMapper baseMapper;
    private final LzshBusinessInfoMapper infoMapper;
    /**
     * 查询商机填报系统菜单
     *
     * @param id 主键
     * @return 商机填报系统菜单
     */
    @Override
    public LzshBusinessMenuVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询商机填报系统菜单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商机填报系统菜单分页列表
     */
    @Override
    public TableDataInfo<LzshBusinessMenuVo> queryPageList(LzshBusinessMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LzshBusinessMenu> lqw = buildQueryWrapper(bo);
        Page<LzshBusinessMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商机填报系统菜单列表
     *
     * @param bo 查询条件
     * @return 商机填报系统菜单列表
     */
    @Override
    public List<LzshBusinessMenuVo> queryList(LzshBusinessMenuBo bo) {
        LambdaQueryWrapper<LzshBusinessMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LzshBusinessMenu> buildQueryWrapper(LzshBusinessMenuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LzshBusinessMenu> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMenuName()), LzshBusinessMenu::getMenuName, bo.getMenuName());
        lqw.eq(StringUtils.isNotBlank(bo.getImgUrl()), LzshBusinessMenu::getImgUrl, bo.getImgUrl());
        lqw.eq(bo.getTemplateId() != null, LzshBusinessMenu::getTemplateId, bo.getTemplateId());
        return lqw;
    }

    /**
     * 新增商机填报系统菜单
     *
     * @param bo 商机填报系统菜单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LzshBusinessMenuBo bo) {
        LzshBusinessMenu add = MapstructUtils.convert(bo, LzshBusinessMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商机填报系统菜单
     *
     * @param bo 商机填报系统菜单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LzshBusinessMenuBo bo) {
        LzshBusinessMenu update = MapstructUtils.convert(bo, LzshBusinessMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LzshBusinessMenu entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除商机填报系统菜单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    // 查询每个菜单的填报数量
    public LzshBusinessMenuNum getMenuNum() {
        // 获取所有菜单
        List<LzshBusinessMenu> menus = baseMapper.selectList(null); // 获取所有菜单数据
        List<LzshBusinessMenuNum.menuNum> listData = new ArrayList<>();
        // 遍历所有菜单
        for (LzshBusinessMenu menu : menus) {
            String menuName = menu.getMenuName();
            Long menuId = menu.getId();
            // 查询每个菜单的填报数量
            Integer count = Math.toIntExact(infoMapper.selectCount(new LambdaQueryWrapper<LzshBusinessInfo>()
                .eq(LzshBusinessInfo::getMenuId, menuId)));
            LzshBusinessMenuNum.menuNum  data= new LzshBusinessMenuNum.menuNum();
            data.setProduct(menuName);
            data.setData1((long) count);
            listData.add(data);
        }
        LzshBusinessMenuNum menuNum = new LzshBusinessMenuNum();
        menuNum.setSource(listData);
        return menuNum;
    }
}
